package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.RedisService;
import cn.itsource.basic.util.TreeUtil;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service //spring管理
public class DepartmentServiceImpl extends BaseServiceImpl<Department,DepartmentQuery> implements IDepartmentService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    public PageList<Department> findPage(DepartmentQuery query) {

        long l1 = System.currentTimeMillis();

        PageList<Department> redisPageList = redisService.getKeyObjectValue("departments", PageList.class);
        if (redisPageList != null){
            System.out.println("走的时候redis的缓存1111111111111111111111111");
            long l2 = System.currentTimeMillis();
            System.out.println("走缓存执行的毫秒数："+(l2-l1));
            return redisPageList;
        }

        //1.分页插件的使用
        PageHelper.startPage(query.getPage(),query.getSize());

        //2.执行分页语句 1，2顺序不能反  ，中间不能写代码
        List<Department> departments = departmentMapper.findPage(query);
        long l3 = System.currentTimeMillis();
        System.out.println("走数据库执行的毫秒数："+(l3-l1));

        //3.封装到pageInfo里面去
        PageInfo<Department> pageInfo = new PageInfo<>(departments);

        //4.封装自定义pageList对象
        PageList<Department> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
        System.out.println("走的mysql数据库222222222222222222");
        redisService.setStringKeyAndValue(10,"departments",pageList);


        return pageList;
    }

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.insertSelective(department);
        //调用update
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {

        //1.获取到级联的数组值
        Long[] parentIds = department.getParentIds(); //[3,7]

        if (parentIds != null && parentIds.length > 0){
            //设置上级id
            department.setParentId(parentIds[parentIds.length-1]);

            StringBuilder sb = new StringBuilder("");
            for (Long parentId : parentIds) {
                sb.append("/").append(parentId);
            }
            sb.append("/").append(department.getId());
            department.setDirPath(sb.toString());
        }else{
            department.setDirPath("/"+department.getId());
        }

        //只有设置了值的字段才会被修改
        departmentMapper.updateByPrimaryKeySelective(department);
    }

    @Override
    public List<Department> deptTree() {
        //1.查询所有的部门数据，作为递归的基础数据
        List<Department> departments = departmentMapper.deptTree();
        //2.通过递归工具类，解析成无限极数据
        List<Department> tree = TreeUtil.getTree(departments);

        return tree;
    }
}
